<template>
	<el-dialog :title="dialogTitle" :visible.sync="visible" top="0" width="992px" append-to-body>
		<el-form v-bind="formConfig" :model="formData" :rules="formRules" label-width="80px" v-loading="loading">
			<el-row :gutter="16">
				<el-col :span="8">
					<el-form-item label="所属题库" prop="bankId">
						<el-select v-model="formData.bankId" filterable placeholder="请选择题库" style="width: 100%">
							<el-option v-for="(item, index) in list" :key="index" :label="item.name" :value="item.id"></el-option>
						</el-select>
					</el-form-item>
				</el-col>
				<el-col :span="8">
					<el-form-item label="试题类型" prop="type">
						<el-select v-model="formData.type" placeholder="请选择试题类型" :disabled="!addOrEdit" style="width: 100%">
							<el-option v-for="(item, index) in subjectTypes" :key="index" :label="item.label" :value="item.value"></el-option>
						</el-select>
					</el-form-item>
				</el-col>
				<el-col :span="8">
					<el-form-item label="试题难度" prop="difficulty">
						<el-select v-model="formData.difficulty" placeholder="请选择试题难度" style="width: 100%">
							<el-option v-for="(item, index) in subjectDifficultys" :key="index" :label="item.label" :value="item.value"></el-option>
						</el-select>
					</el-form-item>
				</el-col>
				<el-col :span="8">
					<el-form-item label="试题来源" prop="source">
						<el-input v-model="formData.source" placeholder="请输入试题来源"></el-input>
					</el-form-item>
				</el-col>
				<el-col :span="8">
					<el-form-item label="试题标签" prop="tags">
						<el-input v-model="formData.tags" placeholder="请输入试题标签"></el-input>
					</el-form-item>
				</el-col>
				<el-col :span="8">
					<el-form-item label="试题状态" prop="status">
						<el-radio-group v-model="formData.status" v-removeAriaHidden:radio>
							<el-radio label="1">正常</el-radio>
							<el-radio label="2">禁用</el-radio>
						</el-radio-group>
					</el-form-item>
				</el-col>
				<el-col :span="24">
					<el-form-item label="试题标题" prop="title">
						<sw-tiny-editor ref="title" v-model="formData.title" type="question" @delete="handleDeleteBlank" v-if="!!visible" :blank="formData.type === '4'" />
					</el-form-item>
				</el-col>
				<el-col :span="24" v-if="!!formData.type">
					<el-form-item :label="answerLabel" prop="subAnswer">
						<subject-answer ref="answer" v-model="formData.subAnswer" :type="formData.type" :options.sync="formData.subjectOptionsVOList" />
					</el-form-item>
				</el-col>
				<el-col :span="24">
					<el-form-item label="题目解析" prop="subAnalysis">
						<sw-tiny-editor v-model="formData.subAnalysis" type="question" v-if="!!visible"></sw-tiny-editor>
					</el-form-item>
				</el-col>
			</el-row>
		</el-form>
		<template slot="footer">
			<el-button size="medium" @click="visible = false">取消</el-button>
			<el-button size="medium" type="primary" :loading="btnLoading" @click="handleSubmitForm()">{{ formTypeName }}</el-button>
		</template>
	</el-dialog>
</template>

<script>
import subjectAnswer from './answer';
import formMixins from '@/mixins/formMixins';
import { subjectType, subjectDifficulty } from '@/utils/subjectData';
import { queryQuestions, addedQuestions, updateQuestions } from '@/api/exam/testSubjects';
export default {
	mixins: [formMixins],
	components: { subjectAnswer },
	props: {
		list: { type: Array, default: () => [] }
	},
	data() {
		return {
			formName: '试题',
			formRules: {
				bankId: [{ required: true, message: '请选择题库', trigger: 'change' }],
				type: [{ required: true, message: '请选择试题类型', trigger: 'change' }],
				status: [{ required: true, message: '请选择试题类型', trigger: 'change' }],
				difficulty: [{ required: true, message: '请选择试题难度', trigger: 'change' }],
				title: [{ required: true, message: '请输入试题标题', trigger: ['blur', 'change'] }]
			}
		};
	},
	computed: {
		detailData() {
			return eval(queryQuestions);
		},
		callFunction() {
			return eval(this.addOrEdit ? addedQuestions : updateQuestions);
		},
		answerLabel() {
			return this.formData.type === '5' ? '参考答案' : '答案设置';
		},
		subjectTypes() {
			return subjectType;
		},
		subjectDifficultys() {
			return subjectDifficulty;
		},
		submitParams() {
			let params = { ...this.formData };
			params['subjectOptionsVOList'] = params.subjectOptionsVOList || [];
			return { ...params, title: this.$removeEmptyTag(params.title) };
		}
	},
	mounted() {
		this.$bus.$on('addedSubjectBlank', (key) => {
			this.$refs['title'].handleInsertText(key);
		});
		this.$bus.$on('deleteSubjectBlank', (key) => {
			this.$refs['title'].handleDeleteBlock(key);
		});
	},
	destroyed() {
		this.$bus.$off('addedSubjectBlank');
		this.$bus.$off('deleteSubjectBlank');
	},
	methods: {
		handleDeleteBlank(key) {
			this.$refs['answer'].handleDeleteBlank(key);
		}
	}
};
</script>
<style lang="scss" scoped>
.dialog_form_wrapper {
	overflow: initial;
}
</style>
